package com.mcc.app.msg.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.system.service.ISysUserService;
import com.mcc.websocket.service.IWebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.msg.mapper.MsgRemindMapper;
import com.mcc.app.msg.domain.MsgRemind;
import com.mcc.app.msg.service.IMsgRemindService;

/**
 * 消息提醒Service业务层处理
 *
 * @author gxy
 * @date 2023-12-05
 */
@Service
public class MsgRemindServiceImpl implements IMsgRemindService {

    @Autowired
    private MsgRemindMapper msgRemindMapper;

    @Autowired
    private IWebSocketService webSocketService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询消息提醒
     *
     * @param id 消息提醒ID
     * @return 消息提醒
     */
    @Override
    public MsgRemind selectMsgRemindById(Long id) {
        return msgRemindMapper.selectById(id);
    }

    /**
     * 查询消息提醒列表
     *
     * @param msgRemind 消息提醒
     * @return 消息提醒
     */
    @Override
    public List<MsgRemind> selectMsgRemindList(MsgRemind msgRemind) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(msgRemind)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(msgRemind.getUserId()), "user_id", msgRemind.getUserId());
            queryWrapper.eq(ObjectUtil.isNotEmpty(msgRemind.getMsgType()), "msg_type", msgRemind.getMsgType());
            queryWrapper.like(ObjectUtil.isNotEmpty(msgRemind.getMsgTitle()), "msg_title", msgRemind.getMsgTitle());
            queryWrapper.like(ObjectUtil.isNotEmpty(msgRemind.getMsgContent()), "msg_content", msgRemind.getMsgContent());
            queryWrapper.eq(ObjectUtil.isNotEmpty(msgRemind.getMsgIsSolved()), "msg_is_solved", msgRemind.getMsgIsSolved());
            queryWrapper.ge(ObjectUtil.isNotEmpty(msgRemind.getStartDt()), "msg_dt", msgRemind.getStartDt());
            queryWrapper.le(ObjectUtil.isNotEmpty(msgRemind.getEndDt()), "msg_dt", msgRemind.getEndDt());
        }
        queryWrapper.orderByDesc("msg_dt", "id");
        List<MsgRemind> res = msgRemindMapper.selectList(queryWrapper);
        for (int i = 0; i < res.size(); i++) {
            MsgRemind row = res.get(i);
            row.setUser(sysUserService.selectUserById(row.getUserId()));
        }
        return res;
    }

    /**
     * 新增消息提醒
     *
     * @param msgRemind 消息提醒
     * @return 结果
     */
    @Override
    public int insertMsgRemind(MsgRemind msgRemind) {
        int res = msgRemindMapper.insert(msgRemind);
        if (res > 0) {
            webSocketService.sendMsgRemind("insert");
        }
        return res;
    }

    /**
     * 修改消息提醒
     *
     * @param msgRemind 消息提醒
     * @return 结果
     */
    @Override
    public int updateMsgRemind(MsgRemind msgRemind) {
        int res = msgRemindMapper.updateById(msgRemind);
        if (res > 0) {
            webSocketService.sendMsgRemind("update");
        }
        return res;
    }

    /**
     * 删除消息提醒
     *
     * @param id 消息提醒ID
     * @return 结果
     */
    @Override
    public int deleteMsgRemindById(Long id) {
        int res = msgRemindMapper.deleteById(id);
        if (res > 0) {
            webSocketService.sendMsgRemind("delete");
        }
        return res;
    }

    /**
     * 批量删除消息提醒
     *
     * @param ids 消息提醒ID
     * @return 结果
     */
    @Override
    public int deleteMsgRemindByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        int res = msgRemindMapper.deleteBatchIds(ListUtil.toList(ids));
        if (res > 0) {
            webSocketService.sendMsgRemind("delete");
        }
        return res;
    }

    @Override
    public int getMsgCount() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("msg_is_solved", 0);
        return msgRemindMapper.selectCount(queryWrapper);
    }

    @Override
    public boolean oneHandle(MsgRemind msgRemind) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(ObjectUtil.isNotEmpty(msgRemind.getMsgType()), "msg_type", msgRemind.getMsgType());
        queryWrapper.eq("msg_is_solved", 0);
        MsgRemind updateObj = new MsgRemind();
        updateObj.setMsgIsSolved(1);
        boolean res = msgRemindMapper.update(updateObj, queryWrapper) > 0;
        if (res) {
            webSocketService.sendMsgRemind("update");
        }
        return res;
    }
}
